package com.gy.niuer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gy.niuer.domain.Appointment;
import com.gy.niuer.domain.Patient;
import com.gy.niuer.mapper.SchedulingMapper;
import com.gy.niuer.service.AppointmentService;
import com.gy.niuer.mapper.AppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
*
*/
@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment>
implements AppointmentService{

    @Autowired
    private SchedulingMapper schedulingMapper;

    @Autowired
    AppointmentMapper appointmentMapper;

    @Override
    public void addAppointment(Appointment appointment) {
        appointmentMapper.addAll(appointment);
    }

    @Override
    @Transactional  //事务
    public boolean addAppoint(int schedulId, Appointment appointment) {
        /**
         * 添加预约信息
         * 排班表的count数量+1，同时成功，同时失败
         */
        boolean flag = schedulingMapper.updateCount(schedulId);
        if(flag){
            return appointmentMapper.addAppoint(appointment);
        }
        return flag;
    }

    @Override
    public boolean deleteAppoint(Appointment appointment) {

        int i = appointmentMapper.deleteById(appointment.getId());
        if(i == 0){
            return false;
        }
//        schedulingMapper.updateCountByTimeAndDoctor(appointment.getTime(),appointment.getDoctor().getId());
        return true;
    }

    @Override
    public List<Appointment> selectById(int id) {
        List<Appointment> appointments = appointmentMapper.selectAllById(id);
        return appointments;
    }

    @Override
    public List<Appointment> getAllAppoint(int id) {
        return appointmentMapper.selectAllByPatient(id);
    }

}
